interface https {
	url : string,
	parms : AnyObject,
	methdos : string
}

// const http : https['url'] = 'https://ddzj.hihink.com/api'
const http : https['url'] = 'https://ddzj.hihink.com'
const urlImage : https['url'] = 'https://ddzj.hihink.com'

const getHttpsRequset = (parms) => new Promise((resolve) => {
	requsetFunction(parms).then(async res => {
		resolve(res)
	})
})

const postHttpsRequset = (parms) => new Promise((resolve) => {
	requsetFunction(parms).then(async res => {
		resolve(res)
	})
})

const requsetFunction = (parms : https['parms']) => new Promise((resolve) => {
	uni.request({
		url: http + parms.path,
		data: parms.data,
		method: parms.methods,
		withCredentials: true,
		crossOrigin: true,
		header: {
			'token': uni.getStorageSync('token') ? uni.getStorageSync('token') : '',
		},
		success: (res : AnyObject) => {
			// console.log(res, 'res')
			// if (res.data.code > 200 && res.data.code < 300) {
			status(res.data.code, res.data.msg)
			// }
			if (res.data) {
				resolve(res.data)
			}
		}
	})
})

const status = (status, msg) => {
	if (status > 500 && status < 600) {
		uni.showToast({
			title: '数据库错误，请稍后再试',
			icon: 'none'
		})
	}
	switch (status) {
		case 0 || -1:
			uni.showToast({
				title: msg,
				icon: 'none'
			})
			break;
		case 400 || 401:
			uni.showToast({
				title: '权限不足',
				icon: 'none'
			})
			break;
		case -10: //403
			console.log('xxxxssss')
			uni.showModal({
				title: '未登录',
				content: '登陆失效请重新登陆',
				success: (res) => {
					if (res.confirm) {
						uni.redirectTo({
							url: '/pages/index/login'
						})
					}
					if (res.cancel) {
						uni.showToast({
							title: '您已取消',
							icon: 'none'
						})
					}
				}
			})
			break;
		default:
			break;
	}
}


export {
	getHttpsRequset,
	postHttpsRequset,
	http,
	urlImage
}